﻿(function($, epi) {
epi.activateMobileCenter = function() {

        var temp = {};
        var onloaded = function(e, gadgetInstance) {
            $(".epi-toolTip", this).epiToolTip();

            $(".pagetype-dropdown", gadgetInstance.element).change(function() {

                if ($(this).val()) {
                    LoadStringPropertiesForPageType(gadgetInstance, $(this).val());
                    LoadXhtmlPropertiesForPageType(gadgetInstance, $(this).val());
                }
            });

            $("#deletebutton").click(function() {

                if (confirm("Are you sure you want to delete the custom search?") == true) {
                    alert('Deleted');
                    return true;
                }
                else
                    return false;
            });

        };

        var LoadXhtmlPropertiesForPageType = function(gadgetInstance, pagetype) {
            var routeValues = { action: "GetXhtmlProperties" };
            var actionUrl = gadgetInstance.getActionPath(routeValues);
            gadgetInstance.ajax({
                url: actionUrl,
                data: "pagetype=" + pagetype,
                dataType: "json",
                success: function(data) {
                    fill(gadgetInstance, data, '.xhtml-dropdown')
                }
            });
        };

        var LoadStringPropertiesForPageType = function(gadgetInstance, pagetype) {
            var routeValues = { action: "GetStringProperties" };
            var actionUrl = gadgetInstance.getActionPath(routeValues);
            gadgetInstance.ajax({
                url: actionUrl,
                data: "pagetype=" + pagetype,
                dataType: "json",
                success: function(data) {
                    fill(gadgetInstance, data, '.string-dropdown')
                }
            });
        };

        var fill = function(gadgetInstance, json, cssclass) {

            var options = $(cssclass);
            options.empty();


            $.each(json, function() {

                options.append(new Option(this.Name, this.Name));
            });

        };


        temp.init = function(e, gadgetInstance) {
            $(this).bind("epigadgetloaded", onloaded);
        };

        return temp;
    } ();
} (epiJQuery, epi));